package bintree.map_set.leetcode;

import java.util.HashMap;
import java.util.Map;

public class Num290单词规律 {
    public boolean wordPattern(String pattern, String s) {
        String[] str = s.split(" ");
        if (str.length!=pattern.length()){
            return false;
        }
        int count = 1;
        Map<String,Integer>map1 = new HashMap<>();
        StringBuilder sb=new StringBuilder();
        for (String temp : str){
            if (map1.containsKey(temp)){
                sb.append(map1.get(temp));
            }else {
                map1.put(temp,count);
                sb.append(count++);
            }
        }
        Map<Character,Integer>map2 = new HashMap<>();
        StringBuilder sb2 = new StringBuilder();
         count = 1;
        for (int i = 0; i < pattern.length(); i++) {
            char c = pattern.charAt(i);
            if (map2.containsKey(c)){
                sb2.append(map2.get(c));
            }else {
                map2.put(c,count);
                sb2.append(count++);
            }
        }
        return sb.toString().equals(sb2.toString());
    }
}
